﻿Public Class ClientForm
    Friend WithEvents InputTextBox As New TextBox() With {.Dock = DockStyle.Bottom}
    Friend WithEvents OutputTextBox As New TextBox() With {.Dock = DockStyle.Fill, .Multiline = True, .ReadOnly = True}
    Friend WithEvents SendButton As New Button() With {.Text = "Send", .Dock = DockStyle.Bottom}

    Private Sub ClientForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Winsock1.Close()
    End Sub

    Private Sub ClientForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Controls.Add(OutputTextBox)
        Controls.Add(InputTextBox)
        Controls.Add(SendButton)
        Winsock1.Connect(System.Net.IPAddress.Loopback, 123)
    End Sub

    Private Sub SendButton_Click(sender As Object, e As System.EventArgs) Handles SendButton.Click
        'Clients use the Write() method to send data to the server.
        Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(InputTextBox.Text)
        Winsock1.Write(data)
    End Sub

    Private Sub Winsock1_DataReceived(sender As Object, e As NetWinsock.DataReceivedEventArgs) Handles Winsock1.DataReceived
        'Clients recieve responses from servers in the DataReceived event.
        Dim dataString As String = System.Text.Encoding.ASCII.GetString(e.ReceivedData.Data)
        OutputTextBox.AppendText(dataString)
        OutputTextBox.AppendText(ControlChars.NewLine)
    End Sub

    Private Sub Winsock1_WinsockError(sender As Object, e As NetWinsock.WinsockErrorEventArgs) Handles Winsock1.WinsockError
        MessageBox.Show(e.Exception.Message)
    End Sub
End Class
